package com.example.no1admin.service.impl;

import com.example.no1admin.ex.ServiceException;
import com.example.no1admin.mapper.PackagesMapper;
import com.example.no1admin.mapper.PhotographerMapper;
import com.example.no1admin.mapper.RouteMapper;
import com.example.no1admin.mapper.StyleMapper;
import com.example.no1admin.pojo.dto.packages.PackagesAddNewDTO;
import com.example.no1admin.pojo.dto.packages.PackagesEditDTO;
import com.example.no1admin.pojo.entity.Packages;
import com.example.no1admin.pojo.vo.packages.PackageMoreVO;
import com.example.no1admin.pojo.vo.packages.PackagesDetailListItemVO;
import com.example.no1admin.pojo.vo.packages.PackagesDetailVO;
import com.example.no1admin.pojo.vo.packages.PackagesListItemVO;
import com.example.no1admin.pojo.vo.photographer.PhotographerDetailVO;
import com.example.no1admin.pojo.vo.route.RouteDetailVO;
import com.example.no1admin.pojo.vo.style.StyleDetailVO;
import com.example.no1admin.service.IPackagesService;
import com.example.no1admin.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class PackagesServiceImpl implements IPackagesService {
    @Autowired
    PackagesMapper packagesMapper;
    @Autowired
    StyleMapper styleMapper;
    @Autowired
    RouteMapper routeMapper;
    @Autowired
    PhotographerMapper photographerMapper;


    public PackagesServiceImpl(){
        log.debug("创建业务逻辑对象：PackagesServiceImpl");
    }

    @Override
    public void addNew(PackagesAddNewDTO packagesAddNewDTO) {
        log.debug("开始处理添加套餐的业务，参数：{}", packagesAddNewDTO);

        // 根据 套餐名字查询是否有重复的
        String packageName =packagesAddNewDTO.getName();
        int count = packagesMapper.countByName(packageName);
        if (count>0){
            String message = "创建套餐失败，套餐名={"+packageName+"}已经存在了!";
            log.error(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }

        // 创建实体对象（Mapper的方法的参数是实体类型）
        Packages packages = new Packages();
        // 将当前方法参数的值复制到Brand实体类型的对象中
        BeanUtils.copyProperties(packagesAddNewDTO, packages);
        // 补全属性值（不由客户端提交的属性的值，应该在插入之前补全）

        // 将套餐数据写入到数据库中
        log.debug("即将向表中写入数据：{}", packages);
        int rows = packagesMapper.insert(packages);
        if (rows != 1) {
            String message = "创建套餐失败，服务器忙，请稍后再次尝试！";
            log.error(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
    }

    @Override
    public void deleteById(Long id) {
        log.debug("开始处理删除套餐的业务,id={}",id);

        PackagesDetailVO packages = packagesMapper.getById(id);
        if(packages==null){
            String message = "删除套餐失败，尝试删除的数据【"+id+"】不存在！";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        int rows = packagesMapper.deleteById(id);
        if(rows!=1){
            String message = "删除套餐失败，服务器忙，请稍后再试!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }
    }

    @Override
    public void updateById(PackagesEditDTO packagesEditDTO) {
        log.debug("开始处理关于修改 套餐数据的逻辑层");
        int count = packagesMapper.updateById(packagesEditDTO);
        if (count!=1){
            String message = "删除套餐失败，服务器忙，请稍后再试!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }
    }

    @Override
    public List<PackagesDetailListItemVO> list() {

        List<PackagesListItemVO> list = packagesMapper.list();
        List<PackagesDetailListItemVO> packagesList =new ArrayList<>();

        // 遍历每个套餐
        for(PackagesListItemVO pa : list){
            PackagesDetailListItemVO  packages  =new PackagesDetailListItemVO();
            BeanUtils.copyProperties(pa,packages);
            // 查每个套餐风格名称
            Long id = pa.getStyleId();
            StyleDetailVO byId = styleMapper.getById(id);
            if (byId !=null) {
                String name = byId.getName();
                packages.setStyleName(name);
            }
            // 查每个套餐路线名称
            Long routeId = pa.getRouteId();
            RouteDetailVO byId1 = routeMapper.getById(routeId);
            if (byId1 !=null) {
                String routeName = byId1.getName();
                packages.setRouteName(routeName);
            }
            // 查每个套餐摄影师名称
            Long photographerId = pa.getPhotographerId();
            PhotographerDetailVO byId2 = photographerMapper.getById(photographerId);
            if (byId2 !=null) {
                String photographerName = byId2.getName();
                packages.setPhotographerName(photographerName);
            }
            // 将每个返回类添加到List集合中
        packagesList.add(packages);
        }
        // 返回类的集合
        return packagesList;
    }

    @Override
    public List<PackageMoreVO> listItem() {
       return  packagesMapper.listItem();
    }
}
